<?php
  /***************************************************************
   * @file: main.php
   * @author: Neo
   * @mail: xq262144@gmail.com
   * @date: 
   * @description:
   * this file is the error controlling model
   ***************************************************************/
   

    function report_error($msg) {
        header("HTTP/1.0 500 Internal Server Error");
        trigger_error($msg, E_USER_ERROR);
        die;
    }
      
    function report_notice($msg) {
        header("HTTP/1.0 404 Not Found");
        trigger_error($msg, E_USER_NOTICE);
        die;
    }

    function sys_exception_handler($exception){
        if (error_reporting() == 0) {
            return;
        }
        $err_filter = 
<<<WARNING_MSG
<div style='background-color:#FEC42D; border:3px solid black; font-family:verdana; text-align:center; margin:10px;'><h3>An Uncaught PHP Exception has encounted</h3><p>%s</p><p>In file %s on line %d </p><p>With Stack Trace:</p><pre>%s</pre></div>

WARNING_MSG;
        printf(
            $err_filter,
            $exception->getMessage(),
            $exception->getCode(),
            $exception->getLine(),
            $exception->getTraceAsString()
        );
      }
      
      function sys_error_handler($code, $msg, $file, $line) {
        if (error_reporting() == 0) {
    //       print "(silenced) ";
            return;
        }
        //switch it to kinds of errors
        switch ($code) {
            case E_WARNING: case E_USER_WARNING:
                $err_filter = 
<<<WARNING_MSG
<div style='background-color:#FEC42D; border:3px solid black; font-family:verdana; text-align:center; margin:10px;'><h3>A PHP Warning has encounted</h3><p>%s</p><p>In file %s on line %d </p><p>With Stack Trace:</p><pre>%s</pre></div>

WARNING_MSG;
                break;
            case E_NOTICE: case E_USER_NOTICE:
                $err_filter = 
<<<NOTICE_MSG
<div style='background-color:#A3C159; border:3px solid black; font-family:verdana; text-align:center; margin:10px;'><h3>A PHP Notice has encounted</h3><p>%s</p><p>In file %s on line %d </p><p>With Stack Trace:</p><pre>%s</pre></div>

NOTICE_MSG;
                break;
            default:
                $err_filter = 
<<<NOTICE_MSG
<div style='background-color:#F52D2D; border:3px solid black; font-family:verdana; text-align:center; margin:10px;'><h3>A PHP Error has encounted</h3><p>%s</p><p>In file %s on line %d </p><p>With Stack Trace:</p><pre>%s</pre></div>

NOTICE_MSG;
                break;
            }
            $dummyException = new Exception;
            printf($err_filter, $msg, $file, $line, $dummyException->getTraceAsString());
        }
    set_error_handler('sys_error_handler');
    set_exception_handler('sys_exception_handler');